package com.example.demo.Utils.Create;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import javax.swing.*;
import java.awt.*;
import java.util.Collections;

public class Create {
    public static class InputFrame extends JFrame {
        private final JTextField textField;

        public InputFrame() {
            setTitle("代码生成器");
            setSize(300, 150);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);

            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());

            textField = new JTextField(15);
            textField.setToolTipText("请输入生成的表");
            panel.add(textField, BorderLayout.NORTH);
            JPanel buttonPanel = getjPanel();

            panel.add(buttonPanel, BorderLayout.SOUTH);
            add(panel);

            setVisible(true);
        }

        private JPanel getjPanel() {
            JButton okButton = new JButton("生成");
            okButton.addActionListener(e -> {
                String inputText = textField.getText();
                boolean success = Build(inputText); // 调用生成操作
                if (success) {
                    JOptionPane.showMessageDialog(null, "生成成功!");
                    textField.setText("");
                } else {
                    JOptionPane.showMessageDialog(null, "生成失败，请重试!");
                }
            });

            JButton cancelButton = new JButton("取消");
            cancelButton.addActionListener(e -> {
                dispose(); // 关闭窗口
            });

            JPanel buttonPanel = new JPanel();
            buttonPanel.add(okButton);
            buttonPanel.add(cancelButton);
            return buttonPanel;
        }
    }

    public static Boolean Build(String TableName) {
        final String Father = System.getProperty("user.dir");
        try {
            FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/wzq?characterEncoding=UTF-8&useUnicode=true&useSSL=false", "root", "root")
                    // 全局配置
                    .globalConfig(builder -> {
                        builder.author("Wu") // 设置作者
                                .commentDate("yyyy-MM-dd hh:mm:ss")   //注释日期
                                .outputDir(System.getProperty("user.dir") + "/src/main/java") // 指定输出目录
                                .disableOpenDir() //禁止打开输出目录，默认打开
                        ;
                    })
                    // 包配置
                    .packageConfig(builder -> {
                        builder.controller("Controller." + TableName)
                                .parent("com.example.demo")
                                .pathInfo(Collections.singletonMap(
                                        OutputFile.controller,
                                        Father + "/src/main/java/com/example/demo/Controller/"
                                ));
                        builder.entity("Entity." + TableName)
                                .parent("com.example.demo") // 设置父包名
                                .pathInfo(Collections.singletonMap(
                                                OutputFile.entity,
                                                Father + "/src/main/java/com/example/demo/Entity/"
                                        )
                                );
                        builder.service("Service." + TableName)
                                .parent("com.example.demo")
                                .pathInfo(Collections.singletonMap(
                                                OutputFile.service,
                                                Father + "/src/main/java/com/example/demo/Service/"
                                        )
                                );
                        builder.serviceImpl("ServiceImpl." + TableName)
                                .parent("com.example.demo")
                                .pathInfo(Collections.singletonMap(
                                                OutputFile.serviceImpl,
                                                Father + "/src/main/java/com/example/demo/ServiceImpl/"
                                        )
                                );
                        builder.mapper("Mapper." + TableName)
                                .parent("com.example.demo")
                                .pathInfo(Collections.singletonMap(
                                                OutputFile.mapper,
                                                Father + "/src/main/java/com/example/demo/Mapper/"
                                        )
                                );
                        builder.parent("com.example.demo")
                                .pathInfo(
                                        Collections.singletonMap(
                                                OutputFile.xml,
                                                Father + "/src/main/resources/mapper/" + TableName + "/"
                                        )
                                );
                    })
                    // 策略配置
                    .strategyConfig(builder -> {
                        builder.addInclude(TableName.toLowerCase()) // 设置需要生成的表名
                                .addTablePrefix("sys_") // 设置过滤表前缀
                                .entityBuilder().enableLombok()
                                .enableFileOverride()
                                .naming(NamingStrategy.underline_to_camel);
                        builder.mapperBuilder().enableFileOverride();
                        builder.serviceBuilder()
                                .enableFileOverride()
                                .formatServiceFileName("%sService")
                                .formatServiceImplFileName("%sServiceImpl");
                        // Controller 策略配置
                        builder.controllerBuilder()
                                .enableFileOverride()
                                .enableRestStyle();
                    })
                    .templateConfig((stringStringFunction, builder) -> builder.controller("templates/Controller.vm"))
                    .execute();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(InputFrame::new);
    }
}
